<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<textarea id="stl"></textarea>
<textarea id="result"></textarea>

<input type="button" onclick="stlToShadertoy()" />

</body>
<script>

    var triangleNum = 0;
    function stlToShadertoy() {
        var resultAyyay = [];
        stl = document.getElementById('stl').value;
        var str1 = stl.split('vertex');
        str1.shift();

        for(var i in str1){
            var str2 = str1[i].split(' ');
            resultAyyay.push(str2[1].trim());
            resultAyyay.push(str2[2].trim());
            var str3 = str2[3].split('endloop')[0];
            resultAyyay.push(str3.trim());
        }

        var resultAyyayN = [];
        var str1N = stl.split('normal');
        str1N.shift();

        for(var i in str1N){
            var str2N = str1N[i].split(' ');
            resultAyyayN.push(str2N[1].trim());
            resultAyyayN.push(str2N[2].trim());
            var str3N = str2N[3].split('outer')[0];
            resultAyyayN.push(str3N.trim());
        }

        var resultStr = '';
        for(var i=0, trNum = 0; i<resultAyyay.length; i+=9){
            resultStr += 'triangles[' + trNum + '].a = vec3('+ resultAyyay[i] +',' + resultAyyay[i+1] + ','+ resultAyyay[i+2] +');\n'
            resultStr += 'triangles[' + trNum + '].b = vec3('+ resultAyyay[i+3] +',' + resultAyyay[i+4] + ','+ resultAyyay[i+5] +');\n'
            resultStr += 'triangles[' + trNum + '].c = vec3('+ resultAyyay[i+6] +',' + resultAyyay[i+7] + ','+ resultAyyay[i+8] +');\n'

            resultStr += 'triangles[' + trNum + '].n = vec3('+ resultAyyayN[trNum*3] +',' + resultAyyayN[trNum*3+1] + ','+ resultAyyayN[trNum*3+2] +');\n'

            trNum++;
        }
        console.log(resultStr);
        document.getElementById('result').value = resultStr;
    }

</script>
</html>
